package com.changyinBus.web.controller.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.changyinBus.common.utils.ApiUtil;
import com.changyinBus.common.utils.IDGenerator;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.web.controller.service.CsrcBankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("CsrcBankService")
public class CsrcBankServiceImpl implements CsrcBankService {
    private static final Logger log = LoggerFactory.getLogger(CsrcBankServiceImpl.class);
    ////生产环境需要更换，需要到门户注册并申请
    @Value("${ssrcBank.appId}")
    private String appId;
    //生产环境需要更换，需要到门户注册并申请
    @Value("${ssrcBank.secretKey}")
    private String secretKey;
    //证书位置
    @Value("${ssrcBank.certPath}")
    private String certPath;
    //生产环境需要更换，需要到门户注册并申请
    @Value("${ssrcBank.signCert}")
    private String signCert;
    //生产环境需要更换，需要到门户注册并申请
    @Value("${ssrcBank.password}")
    private String password;
    //生产环境需要更换，需要到门户注册并申请
    @Value("${ssrcBank.verifyCert}")
    private String verifyCert;
    @Override
    public boolean verifyUser(String openid) {
        try {
            //每次接口请求都需要更换
            String requestId = String.valueOf(IDGenerator.getID());
            String version = "1.0";
            String encryptType = "AES";

            String algorithm = "SHA256WithRSA";
            //如下请求只是下单请求示例，具体请以接口文档为准
            Map<String, Object> params = new HashMap();
            params.put("userid", openid);
            params.put("requestId", requestId);
            params.put("appId", appId);
            String dataText = JSON.toJSONString(params);
            String encryptText = ApiUtil.encryptBySecretKey(encryptType, secretKey, dataText);
            String signText = ApiUtil.signByCertPrivateKey(certPath, signCert, password, algorithm, encryptText);
            Map<String, String> bodyParams = new HashMap();
            bodyParams.put("version", version);
            bodyParams.put("requestId", requestId);
            bodyParams.put("appId", appId);
            bodyParams.put("data", encryptText);
            bodyParams.put("sign", signText);
            //
            String url = "http://apibanktest.csrcbank.com/open/v2/sup/csrcbBus/userIsExist";
            String result = ApiUtil.sendPost(url, bodyParams, 10000, 10000);
            JSONObject jsonObject = JSON.parseObject(result);
            String data = (String)jsonObject.get("data");
            String sign = (String)jsonObject.get("sign");
            if(StringUtils.isBlank(data)) {
                throw new Exception("返回的报文体中无data字段");
            }
            if(StringUtils.isBlank(sign)) {
                throw new Exception("返回的报文体中无sign字段");
            }
            String decryptText = ApiUtil.decryptBySecretKey(encryptType, secretKey, data);
            log.info("decryptText=====================>" + decryptText);
            Boolean verifyResult = ApiUtil.verifyByCertPublicKey(certPath, verifyCert, algorithm, sign, data);
            if(verifyResult){
                JSONObject json = JSONObject.parseObject(decryptText);
                return json.getBoolean("isExist");
            }
        } catch (Exception var22) {
            var22.printStackTrace();
        }
        return false;
    }
}
